#!/bin/bash
# ================================================================
# Maven 批量部署脚本 - Shell版本
# 用途：一键部署所有模块到云端仓库
# 使用方法：
#   ./deploy-all.sh          # 部署到开发环境 (SNAPSHOT)
#   ./deploy-all.sh --prod   # 部署到生产环境 (RELEASE)
# 作者：自动生成
# 日期：2025-07-30
# ================================================================

# 设置错误处理
set -e

# 解析命令行参数
PRODUCTION_MODE=false
if [[ "$1" == "--prod" || "$1" == "--production" ]]; then
    PRODUCTION_MODE=true
fi

# 配置参数
JAVA_HOME_PATH="/d/java/sdk/1.8/jdk1.8.0_351"  # Linux环境下的JDK路径，请根据实际情况修改
MAVEN_SETTINGS="maven-snapshot-settings.xml"
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 根据环境设置部署参数
if [ "$PRODUCTION_MODE" = true ]; then
    DEPLOY_PROFILE="release"
    ENV_NAME="生产环境 (RELEASE)"
    REPO_URL="https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-release-sce47h/"
else
    DEPLOY_PROFILE="snapshot"
    ENV_NAME="开发环境 (SNAPSHOT)"
    REPO_URL="https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-snapshot-ogm1sq/"
fi

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color

echo -e "${GREEN}================================================================${NC}"
echo -e "${GREEN}开始批量部署所有Maven模块到云端仓库${NC}"
echo -e "${GREEN}================================================================${NC}"
echo -e "${YELLOW}项目根目录: $PROJECT_ROOT${NC}"
echo -e "${YELLOW}Java版本: $JAVA_HOME_PATH${NC}"
echo -e "${YELLOW}Maven Settings: $MAVEN_SETTINGS${NC}"
echo -e "${CYAN}部署环境: $ENV_NAME${NC}"
echo -e "${CYAN}目标仓库: $REPO_URL${NC}"
echo ""

# 设置Java环境
export JAVA_HOME="$JAVA_HOME_PATH"
export PATH="$JAVA_HOME_PATH/bin:$PATH"
echo -e "${GREEN}✓ 设置JAVA_HOME: $JAVA_HOME_PATH${NC}"

# 检查Java版本
if ! "$JAVA_HOME_PATH/bin/java" -version >/dev/null 2>&1; then
    echo -e "${RED}✗ Java版本检查失败，请确认JDK路径正确${NC}"
    exit 1
fi
echo -e "${GREEN}✓ Java版本检查通过${NC}"

# 检查Maven Settings文件
if [ ! -f "$PROJECT_ROOT/$MAVEN_SETTINGS" ]; then
    echo -e "${RED}✗ Maven Settings文件不存在: $MAVEN_SETTINGS${NC}"
    exit 1
fi
echo -e "${GREEN}✓ Maven Settings文件检查通过${NC}"

echo ""
echo -e "${CYAN}================================================================${NC}"
echo -e "${CYAN}第一阶段：部署 general-framework 模块组${NC}"
echo -e "${CYAN}================================================================${NC}"

# 进入general-framework目录
cd "$PROJECT_ROOT/general-framework"
echo -e "${YELLOW}当前目录: $(pwd)${NC}"

echo "开始部署 general-framework 模块组..."
start_time=$(date +%s)

if [ "$PRODUCTION_MODE" = true ]; then
    # 生产环境：设置 release 版本并部署到 release 仓库
    mvn -s "../$MAVEN_SETTINGS" versions:set "-DnewVersion=1.0.2-SNAPSHOT" -DprocessAllModules -DgenerateBackupPoms=false
    mvn -s "../$MAVEN_SETTINGS" clean deploy "-Dmaven.test.skip=true" \
        "-DaltSnapshotDeploymentRepository=2565304-release-sce47h::default::https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-release-sce47h" \
        "-DaltReleaseDeploymentRepository=2565304-release-sce47h::default::https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-release-sce47h"
    # 部署完成后恢复 SNAPSHOT 版本
    mvn -s "../$MAVEN_SETTINGS" versions:set "-DnewVersion=1.0.2-SNAPSHOT-SNAPSHOT" -DprocessAllModules -DgenerateBackupPoms=false
else
    # 开发环境：正常部署到 snapshot 仓库
    mvn -s "../$MAVEN_SETTINGS" clean deploy "-Dmaven.test.skip=true"
fi

if [ $? -eq 0 ]; then
    end_time=$(date +%s)
    duration=$((end_time - start_time))
    echo -e "${GREEN}✓ general-framework 模块组部署成功！耗时: ${duration}秒${NC}"
else
    echo -e "${RED}✗ general-framework 模块组部署失败${NC}"
    echo -e "${YELLOW}请检查错误信息并手动修复后重试${NC}"
    exit 1
fi

echo ""
echo -e "${CYAN}================================================================${NC}"
echo -e "${CYAN}第二阶段：部署 chushouya-project 模块组${NC}"
echo -e "${CYAN}================================================================${NC}"

# 进入chushouya-project目录
cd "$PROJECT_ROOT/chushouya-project"
echo -e "${YELLOW}当前目录: $(pwd)${NC}"

echo "开始部署 chushouya-project 模块组..."
start_time=$(date +%s)

if [ "$PRODUCTION_MODE" = true ]; then
    # 生产环境：设置 release 版本并部署到 release 仓库
    mvn -s "../$MAVEN_SETTINGS" versions:set "-DnewVersion=1.0.2-SNAPSHOT" -DprocessAllModules -DgenerateBackupPoms=false
    mvn -s "../$MAVEN_SETTINGS" clean deploy "-Dmaven.test.skip=true" \
        "-DaltSnapshotDeploymentRepository=2565304-release-sce47h::default::https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-release-sce47h" \
        "-DaltReleaseDeploymentRepository=2565304-release-sce47h::default::https://packages.aliyun.com/6888b879e5ba87aaf5003caf/maven/2565304-release-sce47h"
    # 部署完成后恢复 SNAPSHOT 版本
    mvn -s "../$MAVEN_SETTINGS" versions:set "-DnewVersion=1.0.2-SNAPSHOT-SNAPSHOT" -DprocessAllModules -DgenerateBackupPoms=false
else
    # 开发环境：正常部署到 snapshot 仓库
    mvn -s "../$MAVEN_SETTINGS" clean deploy "-Dmaven.test.skip=true"
fi

if [ $? -eq 0 ]; then
    end_time=$(date +%s)
    duration=$((end_time - start_time))
    echo -e "${GREEN}✓ chushouya-project 模块组部署成功！耗时: ${duration}秒${NC}"
else
    echo -e "${RED}✗ chushouya-project 模块组部署失败${NC}"
    echo -e "${YELLOW}请检查错误信息并手动修复后重试${NC}"
    exit 1
fi

# 返回项目根目录
cd "$PROJECT_ROOT"

echo ""
echo -e "${GREEN}================================================================${NC}"
echo -e "${GREEN}🎉 所有模块部署完成！${NC}"
echo -e "${GREEN}================================================================${NC}"
echo "已成功部署的模块组："
echo -e "${GREEN}  ✓ general-framework (5个模块)${NC}"
echo -e "${GRAY}    - general-framework-dependencies${NC}"
echo -e "${GRAY}    - general-framework-core${NC}"
echo -e "${GRAY}    - general-framework-data${NC}"
echo -e "${GRAY}    - general-framework-web${NC}"
echo -e "${GRAY}    - general-framework-job-boot-starter${NC}"
echo ""
echo -e "${GREEN}  ✓ chushouya-project (7个模块)${NC}"
echo -e "${GRAY}    - chushouya-parent${NC}"
echo -e "${GRAY}    - chushouya-common${NC}"
echo -e "${GRAY}    - chushouya-service-manager${NC}"
echo -e "${GRAY}    - chushouya-service-order${NC}"
echo -e "${GRAY}    - chushouya-service-product${NC}"
echo -e "${GRAY}    - chushouya-web-server${NC}"
echo -e "${GRAY}    - chushouya-project${NC}"
echo ""
echo -e "${CYAN}云端仓库地址: $REPO_URL${NC}"
echo ""
echo -e "${YELLOW}所有模块现在都可以通过Maven依赖方式从云端仓库获取！${NC}"

echo ""
echo "脚本执行完成。"
